package com.topwalk.step01;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * 自定义Callable，与上面一样，只是返回FutureTask
 *
 * @author tang
 */
@Slf4j
class Demo2_4FutureTask implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        // 模拟计算需要一秒
        log.debug("进入call方法");
        Thread.sleep(1000);
        log.debug("完成call方法");
        return 2;
    }

    public static void main(String[] args) throws Exception {
        // 使用
        ExecutorService executor = Executors.newCachedThreadPool();
        FutureTask<Integer> futureTask = new FutureTask<>(new Demo2_4FutureTask());
        executor.submit(futureTask);
        log.debug("{}", futureTask.get());
    }

}